﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Utils;

///Author: NHDuc
///Created date: 22/11/2013
namespace VietThuongLMS.Services
{
    public class DoanhThuService
    {
        public static DoanhThuViewModel GetDoanhThuList(int pageNum, int pageIndex)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                List<DoanhThuItemModel> list = context.DoanhThus
                    .Join(context.ChiNhanhs, d => d.MaChiNhanh, c => c.ID, (d,c) => new {DoanhThu = d, ChiNhanh = c})
                    .Select(s => new DoanhThuItemModel
                    {
                        doanhThu = s.DoanhThu,
                        tenChiNhanh = s.ChiNhanh.TenChiNhanh
                    })
                    .OrderByDescending(m => m.doanhThu.ID).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = context.DoanhThus.Count();
                return new DoanhThuViewModel(list, total);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetDoanhThuList", ex.StackTrace, ex.Message);
                return new DoanhThuViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool InsertDoanhThu(DoanhThu comp)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                //DoanhThu dt = context.DoanhThus.FirstOrDefault(d => d.Thang == comp.Thang && d.Nam == comp.Nam
                //                     && d.MaChiNhanh == comp.MaChiNhanh);
                //if (dt == null)
                //{
                    context.AddToDoanhThus(comp);
                //}
                //else
                //{
                //    dt.TienThu = comp.TienThu;
                //    dt.TienChi = comp.TienChi;
                //}
                context.SaveChanges();
                return true;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("DoanhThuServices::InsertDoanhThu", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool UpdateDoanhThu(DoanhThu dt)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                DoanhThu DoanhThu = context.DoanhThus.FirstOrDefault(t => t.ID == dt.ID);
                if (DoanhThu != null)
                {
                    DoanhThu.Ngay = dt.Ngay;
                    DoanhThu.TienThu = dt.TienThu;
                    DoanhThu.TienChi = dt.TienChi;
                    DoanhThu.MaChiNhanh = dt.MaChiNhanh;
                    DoanhThu.LyDo = dt.LyDo;
                    context.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("DoanhThuServices::UpdateDoanhThu", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static DoanhThu GetDoanhThuById(int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                return context.DoanhThus.Where(c => c.ID == id).FirstOrDefault();

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("DoanhThuServices::GetDoanhThuById", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static List<DoanhThu> GetDoanhThuByCNId(int id, int m, int y)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                return context.DoanhThus.Where(c => c.MaChiNhanh == id && c.Ngay.Value.Month == m && c.Ngay.Value.Year == y).ToList();
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("DoanhThuServices::GetDoanhThuByCNId", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool DeleteDoanhThu(int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                DoanhThu obj = context.DoanhThus.Where(c => c.ID == id).FirstOrDefault();
                if (obj != null)
                {
                    context.DeleteObject(obj);
                    context.SaveChanges();
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("DoanhThuServices::DeleteDoanhThu", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }



    }
}